package com.hbsx.why.ui.fujin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbsx.why.NormalMainActivity;
import com.hbsx.why.R;
import com.hbsx.why.TabMainActivity;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;
import com.hbsx.why.base.BaseFragment;
import com.hbsx.why.ui.space.VenueDetailActivity;
import com.hbsx.why.utils.PopMenu;
import com.hbsx.why.utils.refresh.PullLinearLayout;
import com.hbsx.why.utils.refresh.PullRecycleView;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.utils.refresh.listener.ScrollViewListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NearChildPlaceFragment extends BaseFragment {
    public static TabMainActivity myTabActivity;
    @BindView(R.id.my_PullScrollView)
    PullScrollView myPullScrollView;
    @BindView(R.id.my_PullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    @BindView(R.id.no_date)
    PullToRefreshLayout noDatePullToRefreshLayout;
    @BindView(R.id.no_date_linearLayout)
    PullLinearLayout noDateLinearLayout;
    @BindView(R.id.my_PullRecycleView)
    PullRecycleView myPullRecycleView;
    private static String mTitle;
    private static String BUNDLE_flag = "BUNDLE_flag";
    private int flag;
    public static NormalMainActivity myNormalMainActivity;
    private List<String> stringList;
    private int page = 0;
    private Context context;
    private MyAdapter myAdapter;
    private int[] location = new int[2];
    private int[] location2 = new int[2];
    private boolean changeVisible = true, isInitData = false;

    @Override
    protected int getLayout() {
        return R.layout.ui_near_child_place_fragment;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) {
            flag = arguments.getInt(BUNDLE_flag);
        }
        noDateLinearLayout.setPullDownEnable(true);
        noDateLinearLayout.setPullUpEnable(false);
        stringList = new ArrayList<>();
        page = 1;
        getData(page);
        if (stringList.size() > 0) {
            noDatePullToRefreshLayout.setVisibility(View.GONE);
        } else {
            noDatePullToRefreshLayout.setVisibility(View.VISIBLE);
        }
        myAdapter = new MyAdapter(stringList);
        setGridLayout(myPullRecycleView, context, 1);
        myPullRecycleView.setAdapter(myAdapter);
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myAdapter = new MyAdapter(stringList);
                        setGridLayout(myPullRecycleView, context, 1);
                        myPullRecycleView.setAdapter(myAdapter);
                        myPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        myAdapter.addDate(stringList, true);
                        myAdapter.notifyDataSetChanged();
                        myPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });
        noDatePullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myAdapter = new MyAdapter(stringList);
                        setGridLayout(myPullRecycleView, context, 1);
                        myPullRecycleView.setAdapter(myAdapter);
                        noDatePullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }
        });
        /**
         * list的点击事件
         */
        myAdapter.setOnItemClickListener(R.id.text_view_img1,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img2,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img3,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img4,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img5,new Adaperitemclick());
    }

    class Adaperitemclick implements LGRecycleViewAdapter.ItemClickListener {

        @Override
        public void onItemClicked(View view, int position) {
            startActivity(new Intent(context,VenueDetailActivity.class));
        }
    }

    @Override
    public void fetchData() {
        if (!isInitData) {
            page = 1;
            getData(page);
            myAdapter.refreshDate(stringList);
            myAdapter.notifyDataSetChanged();
        }
    }

    private void getData(int page) {
        if (page == 1) {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                if (i % 5 == 0) {
                    stringList.add("安庆市图书馆");
                }
                if (i % 5 == 1) {
                    stringList.add("安庆市文化馆");
                }
                if (i % 5 == 2) {
                    stringList.add("桐城市文化馆");
                }
                if (i % 5 == 3) {
                    stringList.add("安庆市美术馆");
                }
                if (i % 5 == 4) {
                    stringList.add("安庆博物馆");
                }
            }
            if (stringList.size() > 0) {
                noDatePullToRefreshLayout.setVisibility(View.GONE);
            } else {
                noDatePullToRefreshLayout.setVisibility(View.VISIBLE);
            }
        } else {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                stringList.add(flag + "加载数据--" + page + i);
            }
        }
    }

    public class MyAdapter extends LGRecycleViewAdapter<String> {
        private boolean flag = false;
        private final int typetop = 1, typecenter = 2, typebottom = 3, nodata = 4;
        private List<String> dataList;
        private TextView textViewlist;
        public MyAdapter(List<String> dataList) {
            super(dataList);
            this.dataList = dataList;

        }


        public void refreshDate(List<String> dataList, boolean flag) {
            super.refreshDate(dataList);
            this.flag = flag;
        }

        public void addDate(List<String> dataList, boolean flag) {
            super.addDate(dataList);
            this.flag = flag;
        }

        @Override
        public int getItemViewType(int position) {
            switch (position) {
                case 0:
                    position = typetop;
                    break;
                case 1:
                    position = typecenter;
                    break;
                case 2:
                    position = typebottom;
                    break;
                default:
                    position = typebottom;
                    break;
            }
            return position;
        }

        @Override
        public int getLayoutId(int viewType) {
            switch (viewType) {
                case typetop:
                    viewType = R.layout.item_space_list_top;
                    break;
                case typecenter:
                    viewType = R.layout.item_near_space_list_bottom;
                    break;
                case typebottom:
                    viewType = R.layout.item_near_space_list_bottom;
                    break;
                default:
                    viewType = R.layout.item_near_space_list_bottom;
                    break;
            }
            return viewType;
        }


        @Override
        public void convert(LGViewHolder holder, String s, int position) {
            switch (position) {
                case 0:

                    break;
                case 1:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    break;
                case 2:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    break;
                default:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    break;
            }
//            textViewlist.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    startActivity(new Intent(context, VenueDetailActivity.class));
//                }
//            });
        }
    }


    public static NearChildPlaceFragment newInstance() {
        NearChildPlaceFragment fragment = new NearChildPlaceFragment();
        return fragment;
    }

    public static NearChildPlaceFragment newInstance(String title, int flag) {
        NearChildPlaceFragment fragment = new NearChildPlaceFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_flag, flag);
        mTitle = title;
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * GridLayoutManager
     */

    public void setGridLayout(PullRecycleView pullRecycleView, Context mContext, int spanCount) {
        CustomGridLayoutManager gridLayoutManager = new CustomGridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        gridLayoutManager.setScrollEnabled(false);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }

    public class CustomGridLayoutManager extends GridLayoutManager {
        private boolean isScrollEnabled = true;

        public CustomGridLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }

        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }
}
